package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DcareDao;
import cn.gson.fengboot.model.dao.DfcareDao;
import cn.gson.fengboot.model.dao.DsetcareDao;
import cn.gson.fengboot.model.mapper.DcareMapper;
import cn.gson.fengboot.model.mapper.DsetcareMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class DcareService {


    @Autowired
    DcareMapper dcareMapper;

    @Autowired
    DcareDao dcareDao;

    @Autowired
    DsetcareMapper dsetcareMapper;

    @Autowired
    DsetcareDao dsetcareDao;

    @Autowired
    DfcareDao dfercareDao;

    @Autowired
    DloginService dloginService;


    /*查询生日祝福列表*/
    public List<TfylinkmanForm> selectlinkman(HttpServletRequest request, String birarr){
        List<Integer> getempIdlist = dloginService.getempidlist(request);

        List<TfylinkmanForm> tfylinkmanFormList = null;

        tfylinkmanFormList = new ArrayList<>();
        if(getempIdlist.size()>0){/*根据员工名称查询*/
            for (int i=0;i<getempIdlist.size();i++){
                List<TfylinkmanForm> list = list(getempIdlist.get(i),birarr);
                tfylinkmanFormList.addAll(list);//添加每一个成员的数据
            }
        }else {/*查询所有*/
            List<TfylinkmanForm> list = list(null,birarr);
            tfylinkmanFormList.addAll(list);//添加每一个成员的数据
        }


        return tfylinkmanFormList;
    }

    public List<TfylinkmanForm> list(Integer empId,String birarr){
        String str1="";
        String str2="";
        if(birarr=="" || birarr.equals("undefined")){
            return dcareMapper.selectlinkman(empId,str1,str2);
        }
        if(!birarr.equals("undefined")){
            String[] a = new String[2];
            a = birarr.split(",");
            str1=a[0];
            str2=a[1];
            return dcareMapper.selectlinkman(empId,str1,str2);
        }
        return dcareMapper.selectlinkman(empId,str1,str2);
    }

    /*查询生日模板*/
    public Dsetcare selectsetce() {
        Integer setcaId = 1;
        return dsetcareMapper.selectsetce(setcaId);
    }

    /*查询节日模板*/
    public Dsetcare selectfestivalsetce() {
        Integer setcaId = 2;
        return dsetcareMapper.selectsetce(setcaId);
    }



    public void editsetca(Dsetcare dsetcare) {
        dsetcareDao.save(dsetcare);
    }


    public List<TfylinkmanForm> selectneed(){
        Integer flag = 1;
        return dcareMapper.selectneed(flag);
    }

    public List<TfylinkmanForm> selectallneed(){
        return dcareMapper.selectneed(0);
    }


    /*保存生日发送记录*/
    public void save(Dcare dcare){
        dcareDao.save(dcare);
    }
    /*保存节日发送记录*/
    public void savefer(Dfercare dfercare){
        dfercareDao.save(dfercare);
    }

    public List<Dcare> selectcare(String setarr) {
        String str1="";
        String str2="";
        if(setarr == null ||  ("".equals(setarr))){
            return dcareMapper.selectcare(str1,str2);
        }
        String[] a = new String[2];
        a = setarr.split(",");
        str1=a[0];
        str2=a[1];
        return dcareMapper.selectcare(str1,str2);
    }

    public void editfestivalsetcare(Dsetcare dsetcare) {
        dsetcareDao.save(dsetcare);
    }
}
